<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="1000px"
      @closed="resetForm('form')"
      :lock-scroll="false"
      :close-on-click-modal="false"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="60px">
          <el-form-item label="" prop="content">
            <UEditor ref="ueditor" :ueHeight="300" :defaultMsg="form.content" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  tbSysFeedbackAdd,
  tbSysFeedUpdate,
} from '@/api/admin/system/feedback.js'
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {
      title: '填写意见',
      dialogVisible: false,
      addOrEdit: 1,
      checkedData: {},
      form: {
        status: '0',
        content: '',
      },
      rules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        content: [
          { required: true, message: '请输入意见描述', trigger: 'blur' },
        ],
      },
    }
  },
  mounted() {
    Bus.$on('showDialogFeedbackAdd', (v) => {
      if (v) {
        this.checkedData = v
        this.handleAddOrEdit(2)
      } else {
        this.handleAddOrEdit(1)
      }
    })
  },
  beforeDestroy() {
    Bus.$off('showDialogFeedbackAdd')
  },
  methods: {
    //显示弹窗
    handleAddOrEdit(type) {
      if (type == 2) {
        this.title = '回复意见'
        for (let i in this.checkedData) {
          if (this.form.hasOwnProperty(i)) {
            this.form[i] = this.checkedData[i] || ''
          }
        }
        this.form.id = this.checkedData.id
        this.form.status = 1
      } else {
        this.title = '填写意见'
        delete this.form.id
      }
      this.addOrEdit = type
      this.dialogVisible = true
    },
    submitForm(form) {
      this.form.content = this.$refs.ueditor.message
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.addOrEdit == 1 ? this.add() : this.edit()
        }
      })
    },
    //新增
    add() {
      tbSysFeedbackAdd(this.form).then((res) => {
        this.dialogVisible = false
        Bus.$emit('refreshFeedbackList')
        this.$message({
          message: '提交成功',
          type: 'success',
        })
      })
    },
    //编辑
    edit() {
      tbSysFeedUpdate(this.form).then((res) => {
        this.dialogVisible = false
        Bus.$emit('refreshFeedbackList')
        this.$message({
          message: '操作成功',
          type: 'success',
        })
      })
    },
    //重置表单
    resetForm(form) {
      this.$refs.ueditor.message = ''
      this.$refs[form].resetFields()
      for (let key in this.form) {
        this.form[key] = ''
        this.form.status = '0'
      }
    },
  },
}
</script>